// HisPastPage.cpp : implementation file
//

#include "StdAfx.h"
#include "../Frame/GYWalkAidApp.h"
#include "HisPastPage.h"
#include "PatMan.h"
#include "../Common/CommonFun.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHisPastPage dialog


CHisPastPage::CHisPastPage(CWnd* pParent /*=NULL*/)
	: CPageDialog(CHisPastPage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHisPastPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CHisPastPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHisPastPage)
	DDX_Control(pDX, IDC_EDIT_PASTHIST, m_edtPastHist);
	DDX_Control(pDX, IDB_PAST_SAVE, m_btnPastHistSave);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CHisPastPage, CDialog)
	//{{AFX_MSG_MAP(CHisPastPage)
	ON_BN_CLICKED(IDB_PAST_SAVE, OnPastSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHisPastPage message handlers

void CHisPastPage::OnPastSave() 
{
	if(GYMsgBox(IDS_PAT_CONFIRM_SAVE, MB_YESNO|MB_ICONQUESTION) != IDYES)
		return;
	
	CPatMan* pParent = (CPatMan*)GetParent();
	if(pParent)
	{
		if(!pParent->SavePatMedInfo())
			return;
		
		CString strMRN = GetApp()->GetCurMRN();
		if(!SaveData(strMRN))
			return;

		pParent->EnableTab(TAB_INDEX_BODYCHECK, TRUE);
		pParent->SelectTab(TAB_INDEX_BODYCHECK);

		CBodyCheckPage* pBodyCheckPage =  (CBodyCheckPage*)pParent->GetTabPage(TAB_INDEX_BODYCHECK);
		if(pBodyCheckPage == NULL)
			return;
	
		pBodyCheckPage->NewFirstBodyCheck();
		pBodyCheckPage->EnableInputControls(TRUE);
	}
}

BOOL CHisPastPage::SaveData(CString strMRN)
{
	CString strSQL;
	strSQL.Format(_T("SELECT MedicalRecordNo FROM PatMedInfoText WHERE MedicalRecordNo='%s' AND InfoType=%d"), strMRN, MedicalInfoType);
	
	CGYDataSet tblInfoText(strSQL);
	if(!tblInfoText.IsEOF())
		return TRUE;

	CTime curTime = CTime::GetCurrentTime();
	CString strCurTime;
	strCurTime.Format(_T("%4d-%02d-%02d"), curTime.GetYear(), curTime.GetMonth(), curTime.GetDay());
	
	CString strText = m_edtPastHist.GetControlData(FALSE);
	strSQL.Format(_T("INSERT INTO PatMedInfoText (MedicalRecordNo, InfoDate, InfoType, InfoText) VALUES ('%s', '%s', %d, '%s')"), strMRN, strCurTime, MedicalInfoType, strText);
	if(!tblInfoText.ExecuteSQL(strSQL))
	{
		DataErrorMsgBox(__FILE__, __LINE__, strSQL, tblInfoText.GetLastErrorString());
		return FALSE;
	}
	
	strSQL.Format(_T("UPDATE PatientInfo SET DiagProgress=%d WHERE MedicalRecordNo='%s'"), TAB_INDEX_PASTHIST, strMRN);
	tblInfoText.ExecuteSQL(strSQL);
	
	EnableInputControls(FALSE);
	return TRUE;
}


BOOL CHisPastPage::ShowTableData(CString strMRN)
{
	CString strSQL;
	strSQL.Format(_T("SELECT InfoText FROM PatMedInfoText WHERE MedicalRecordNo='%s' AND InfoType=%d"), strMRN, MedicalInfoType);
	CGYDataSet tblInfoText;
	tblInfoText.OpenTable(strSQL);
	if(tblInfoText.IsEOF())
	{
		m_btnPastHistSave.EnableWindow(TRUE);
		m_edtPastHist.SetWindowText(_T(""));
		return TRUE;
	}
	
	CString strText;
	tblInfoText.GetFieldValue(_T("InfoText"), strText);
	
	m_edtPastHist.SetWindowText(strText);
	m_btnPastHistSave.EnableWindow(FALSE);
	
	return TRUE;
}

BOOL CHisPastPage::ClearData()
{
	m_edtPastHist.SetWindowText(_T(""));
	
	return TRUE;
}

BOOL CHisPastPage::EnableInputControls(BOOL bEnable)
{
	m_btnPastHistSave.EnableWindow(bEnable);
	
	m_edtPastHist.EnableWindow(bEnable);
	m_edtPastHist.SetReadOnly(!bEnable);
	
	CPageDialog::EnableInputControls(bEnable);
	return TRUE;
}